#include "module_window.h"
#include "ui_module_window.h"

#include <QDebug>
#include <QPushButton>

#include "mainwindow.h"

#include "code/code_window.h"

#include <QMessageBox>

module_window::module_window(Module_Data data, Margin win_margin, QWidget *parent) :
    QFrame(parent),
    ui(new Ui::module_window)
{
    ui->setupUi(this);

    this->module_data = data;

    this->init_second_window();

    this->set_margin(win_margin);

}

void module_window::set_margin(int leftMargin, int topMargin, int rightMargin, int bottonMargin)
{
    this->init_module_window_margin(leftMargin, topMargin, rightMargin, bottonMargin);

    ui->gridLayout_first->setContentsMargins(leftMargin, topMargin, rightMargin, bottonMargin);
}

void module_window::set_margin(Margin margin)
{   
    this->init_module_window_margin(margin.leftMargin, margin.topMargin, margin.rightMargin, margin.bottonMargin);

    ui->gridLayout_first->setContentsMargins(margin.leftMargin, margin.topMargin, margin.rightMargin, margin.bottonMargin);
}

void module_window::set_module_name(QString name)
{
    this->module_data.set_module_name(name);
}


void module_window::setLableName(QString name)
{
    this->set_module_name(name);
    this->ModuleLabel->setText(name);

    emit changeLableName(name);
}


///---------------------------------------------------------------
/// 初始化

void module_window::init_module_window_margin(int leftMargin, int topMargin, int rightMargin, int bottonMargin)
{
    this->module_window_margin.leftMargin = leftMargin;
    this->module_window_margin.topMargin = topMargin;
    this->module_window_margin.rightMargin = rightMargin;
    this->module_window_margin.bottonMargin = bottonMargin;
}

void module_window::init_second_window()
{
    this->second_window = new module_secondframe(this);

    connect(second_window, &module_secondframe::clicked, this, &module_window::make_window_property);
    connect(second_window, &module_secondframe::double_clicked, this, &module_window::make_code_window);

    ui->gridLayout_second->addWidget(second_window);
    second_window->show();
}

void module_window::init()
{
    this->init_label();
    this->init_port();
}

void module_window::init_port()
{
    int input = module_data.get_input();
    int output = module_data.get_output();
    int inout = module_data.get_inout();

    int sum = output + inout;
    ///---------------------------------------------------------------
    /// 创建Input Port

    for (int cycle(0); cycle < input ; cycle++) {
        module_port *port = new module_port(this);

        port->set_InOut("Input");
        port->set_name(module_data.ports.Input_List.at(cycle).name);

        this->move_port(port, input, cycle + 1);

        port->show();

        port_list_input.append(port);
    }

    ///---------------------------------------------------------------
    /// 创建Output Port

    for (int cycle(0); cycle < output ; cycle++) {
        module_port *port = new module_port(this);

        port->set_InOut("Output");
        port->set_name(module_data.ports.Output_List.at(cycle).name);

        this->move_port(port, sum, cycle + 1);

        port->show();

        port_list_output.append(port);
    }

    ///---------------------------------------------------------------
    /// 创建InOut Port

    for (int cycle(output); cycle < sum; cycle++) {
        module_port *port = new module_port(this);

        port->set_InOut("InOut");
        port->set_name(module_data.ports.InOut_List.at(cycle - output).name);

        this->move_port(port, sum, cycle + 1);

        port->show();

        port_list_inout.append(port);
    }
}


QString module_window::code_generate()//代码自动生成 还没写完
{
    QString codes;


        codes = codes + this->get_module_name();
        for(int i = 0;i<this->port_list_input.size();i++){
                if(i ==this->port_list_input.size()-1){
                    codes =codes + "\t" + this->port_list_input.at(i)->get_name();
                }else{
                    codes =codes + "\t" + this->port_list_input.at(i)->get_name()+",\n";
                }
            }


    return codes;

}

void module_window::init_label()
{
    ModuleLabel->setObjectName("QLabel_Module_Label");
    ModuleLabel->setText(get_module_name());

    ModuleLabel->setSizeIncrement(50, 50);
    ModuleLabel->raise();

    move_label();



    ModuleLabel->show();
}

///---------------------------------------------------------------
/// 获取

QString module_window::get_module_name()
{
    return this->module_data.get_module_name();
}

QString module_window::get_module_type()
{
    return this->module_data.get_module_type();
}

QString module_window::get_module_class()
{
    return this->module_data.get_module_class();
}

///---------------------------------------------------------------

void module_window::move_port(module_port *port, int sum, int num)
{
    int port_x = 0;
    int port_y = 0;

    int x = port->width() / 2;
    //int y = this->height() - module_window_margin.topMargin - module_window_margin.bottonMargin;
    int y = this->second_window->height() - module_window_margin.topMargin;

    if (port->get_InOut() == "Input")
    {
        port_x = this->module_window_margin.leftMargin - x;
    }
    else
    {
        port_x = this->module_window_margin.leftMargin + this->second_window->width() - x;
    }

    port_y = y * num / (sum + 1);
    port_y += module_window_margin.topMargin;

    port->move(port_x, port_y);
}

void module_window::move_label()
{
    this->ModuleLabel->move(this->width() / 2 - ModuleLabel->width() / 2, this->height() / 2 - ModuleLabel->height() / 2);
}

void module_window::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        module_propertys::delete_propertys();
    }
}

void module_window::mouseDoubleClickEvent(QMouseEvent *event)
{
    ;;
}

void module_window::resizeEvent(QResizeEvent *event)
{
    for (int cycle(0); cycle < port_list_input.length(); cycle++) {
        this->move_port(port_list_input[cycle], port_list_input.length(), cycle + 1);
    }

    for (int cycle(0); cycle < port_list_output.length(); cycle++ ) {
        this->move_port(port_list_output[cycle], module_data.sum_portNumber() - module_data.get_input(), cycle + 1);
    }

    for (int cycle(0); cycle < port_list_inout.length(); cycle++) {
        this->move_port(port_list_inout[cycle], module_data.sum_portNumber() - module_data.get_input(), cycle + port_list_output.length() + 1);
    }

    this->move_label();
}

void module_window::make_window_property()
{
    module_window_property *new_property = new module_window_property(this->module_data);

    QObject::connect(new_property, &module_window_property::changeLableName, this, &module_window::setLableName);

    module_propertys::delete_propertys();

    foreach (QWidget *w, qApp->topLevelWidgets())
    {
        if (MainWindow *mainwin = qobject_cast<MainWindow*>(w))
        {
            mainwin->add_module_property_interface(new_property);
        }
    }

    new_property->show();
}

void module_window::make_code_window()
{
    code_window *c_window = new code_window(this->module_data.get_module_name(), &this->code);
    c_window->show();

}

module_window::~module_window()
{
    delete ui;
}

///---------------------------------------------------------------
/// module 第二层类

module_secondframe::module_secondframe(QWidget *parent) : QFrame(parent)
{
    this->setStyleSheet("background-color: rgb(117, 117, 117); border-radius: 15px;");
}

void module_secondframe::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        emit clicked();
    }
}

void module_secondframe::mouseDoubleClickEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        emit double_clicked();
    }
}

void module_secondframe::resizeEvent(QResizeEvent *event)
{
//    qDebug() << "second" << this->size();
}
